package com.ds.infrastructure.hyperspace.console.dto.apirouter;

import com.ds.infrastructure.hyperspace.console.dto.configdata.ConfigDataDTO;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-09-28 14:52
 **/
@Data
@NoArgsConstructor
public class ApiRouterConfigDTO implements Serializable {
    private Integer version;
    private List<ApiRouterPipelinesDTO> pipelines = new ArrayList<>();
    private ApiRouterEndpointsDTO endpoints = new ApiRouterEndpointsDTO();

    public static ApiRouterConfigDTO toApiRouter(ConfigDataDTO dto, ApiRouterConfigDTO apiRouterConfigDTO) {
        //
        addEndpoints(dto, apiRouterConfigDTO);
        //
        addPipelines(dto, apiRouterConfigDTO);
        return apiRouterConfigDTO;

    }

    private static void addEndpoints(ConfigDataDTO dto, ApiRouterConfigDTO apiRouterConfigDTO) {
        if (dto.getEndpoints() == null || dto.getEndpoints().isEmpty()) {
            throw new HyperspaceException("endpoints is empty");
        }
        ApiRouterEndpointsDTO.toApiRouter(dto.getEndpoints(), apiRouterConfigDTO.getEndpoints());
    }

    private static void addPipelines(ConfigDataDTO dto, ApiRouterConfigDTO apiRouterConfigDTO) {
        if (dto.getPipelines() == null || dto.getPipelines().isEmpty()) {
            throw new HyperspaceException("pipelines is empty");
        }
        List<ApiRouterPipelinesDTO> apiRouterPipelinesDTOList = new ArrayList<>();
        dto.getPipelines().forEach(item -> apiRouterPipelinesDTOList.add(ApiRouterPipelinesDTO.toApiRouter(item)));
        apiRouterConfigDTO.getPipelines().addAll(apiRouterPipelinesDTOList);
    }
}
